﻿/*****************************************************************
// Copyright (C)
// All rights reserved.
// 
// Author:       WYQ
// Create Date:  2012-8-18 15:16:02
// Usage: 持久化数据库仓库
*****************************************************************/
using System;
using System.Linq;
using System.Linq.Expressions;

namespace Portal.Ef.Repository
{


    public class ProductRepository : Repository<Product, Product, TbProduct>, IProductRepository<Product>
    {
        public override Product CreateEntity()
        {
            return new Product { };
        }
        public override IQueryable<Product> Get()
        {
            return MapQueryable(Entities.TbProduct);
        }
        public override long Count()
        {
            return MapQueryable(Entities.TbProduct).Count();
        }
        public override IQueryable<Product> Get(Expression<Func<Product, bool>> expression)
        {
            return MapQueryable(Entities.TbProduct).Where(expression);
        }
        public override IQueryable<Product> GetSort(string sort = "Id", string order = "DESC")
        {
            Check.Argument.IsNotNull(sort, "排序字段");
            Check.Argument.IsNotNull(order, "排序条件");
            return LinqHelper.DataSorting(MapQueryable(Entities.TbProduct), sort, order);
        }
        public override IQueryable<Product> GetSort(Expression<Func<Product, bool>> expression, string sort = "Id", string order = "DESC")
        {
            Check.Argument.IsNotNull(expression, "查询条件");
            Check.Argument.IsNotNull(sort, "排序字段");
            Check.Argument.IsNotNull(order, "排序条件");
            return LinqHelper.DataSorting(MapQueryable(Entities.TbProduct).Where(expression), sort, order);
        }
        public override void Add(Product t)
        {
            Entities.AddToTbProduct(ReverseMapModel(t));
        }
        public override void Update(Product t)
        {
            throw new NotImplementedException();
        }
        public override void AddOrUpdate(Product t)
        {
            if (Entities.TbProduct.Any(o => o.CoProductId == t.Id))
            {

            }
            else
            {
                Entities.AddToTbProduct(ReverseMapModel(t));
            }
        }
        public override void Delete(Product t)
        {
            Entities.TbProduct.Where(o => o.CoProductId == t.Id).ForEach(o => Entities.DeleteObject(o));
        }
        public override void Delete(Expression<Func<Product, bool>> expression)
        {
            MapQueryable(Entities.TbProduct).Where(expression).ForEach(o => Entities.DeleteObject(o));
        }
        public override void Save()
        {
            Entities.SaveChanges();
        }

        public JqEasyuiDataGrid<Product> JqEasyuiDataGrid(string code, string name, string categoryId, string sort = "CreateTime", string order = "Desc", int page = 10, int index = 1)
        {
            return new JqEasyuiDataGrid<Product>();
        }

        public override sealed IQueryable<Product> MapQueryable(IQueryable<TbProduct> queryable)
        {
            return queryable.Select(o => new Product
            {

                Id = o.CoProductId,
                NameEn = o.CoNameEn
,
                Name = o.CoName
,
                CreateTime = o.CoCreateTime ?? DateTime.Now
,
                IsDelete = o.CoIsDelete ?? false
,
                Version = o.CoVersion ?? 0
,
                CategoryId = o.CoCategoryId
,
                DescriptionEn = o.CoDescriptionEn
,
                Description = o.CoDescription
,
                BigImage = o.CoBigImage
,
                NormalImage = o.CoNormalImage
,
                SmallImage = o.CoSmallImage
,
                ModelEn = o.CoModelEn
,
                Model = o.CoModel
,
                IsRecommend = o.CoIsRecommend ?? false
,
                Sort = o.CoSort ?? 0
,
                Code = o.CoCode
,
                Culture = o.CoCulture
,
                Price = o.CoPrice ?? 0
,
                DiscountPrice = o.CoDiscountPrice ?? 0
,
                StockNumber = o.CoStockNumber ?? 0
,
                SoldNumber = o.CoSoldNumber ?? 0
,
                IsClosed = o.CoIsClosed ?? false
,
                Overview = o.CoOverview
,
                OverviewEn = o.CoOverviewEn
,
                Parameters = o.CoParameters
,
                ParametersEn = o.CoParametersEn

            });
        }

        public override sealed IQueryable<TbProduct> ReverseMapQueryable(IQueryable<Product> queryable)
        {
            return queryable.Select(o => new TbProduct
            {

                CoProductId = o.Id,
                CoNameEn = o.NameEn
,
                CoName = o.Name
,
                CoCreateTime = o.CreateTime
,
                CoIsDelete = o.IsDelete
,
                CoVersion = o.Version
,
                CoCategoryId = o.CategoryId
,
                CoDescriptionEn = o.DescriptionEn
,
                CoDescription = o.Description
,
                CoBigImage = o.BigImage
,
                CoNormalImage = o.NormalImage
,
                CoSmallImage = o.SmallImage
,
                CoModelEn = o.ModelEn
,
                CoModel = o.Model
,
                CoIsRecommend = o.IsRecommend
,
                CoSort = o.Sort
,
                CoCode = o.Code
,
                CoCulture = o.Culture
,
                CoPrice = o.Price
,
                CoDiscountPrice = o.DiscountPrice
,
                CoStockNumber = o.StockNumber
,
                CoSoldNumber = o.SoldNumber
,
                CoIsClosed = o.IsClosed
,
                CoOverview = o.Overview
,
                CoOverviewEn = o.OverviewEn
,
                CoParameters = o.Parameters
,
                CoParametersEn = o.ParametersEn


            });
        }

        public override sealed Product MapModel(TbProduct model)
        {
            return new Product
            {
                Id = model.CoProductId,
                NameEn = model.CoNameEn
                ,
                Name = model.CoName
                ,
                CreateTime = model.CoCreateTime ?? DateTime.Now
                ,
                IsDelete = model.CoIsDelete ?? false
                ,
                Version = model.CoVersion ?? 0
                ,
                CategoryId = model.CoCategoryId
                ,
                DescriptionEn = model.CoDescriptionEn
                ,
                Description = model.CoDescription
                ,
                BigImage = model.CoBigImage
                ,
                NormalImage = model.CoNormalImage
                ,
                SmallImage = model.CoSmallImage
                ,
                ModelEn = model.CoModelEn
                ,
                Model = model.CoModel
                ,
                IsRecommend = model.CoIsRecommend ?? false
                ,
                Sort = model.CoSort ?? 0
                ,
                Code = model.CoCode
                ,
                Culture = model.CoCulture
                ,
                Price = model.CoPrice ?? 0
                ,
                DiscountPrice = model.CoDiscountPrice ?? 0
                ,
                StockNumber = model.CoStockNumber ?? 0
                ,
                SoldNumber = model.CoSoldNumber ?? 0
                ,
                IsClosed = model.CoIsClosed ?? false
                ,
                Overview = model.CoOverview
                ,
                OverviewEn = model.CoOverviewEn
                ,
                Parameters = model.CoParameters
                ,
                ParametersEn = model.CoParametersEn
            };
        }

        public override sealed TbProduct ReverseMapModel(Product model)
        {
            return new TbProduct
            {
                CoProductId = model.Id,
                CoNameEn = model.NameEn

                ,
                CoName = model.Name

                ,
                CoCreateTime = model.CreateTime

                ,
                CoIsDelete = model.IsDelete

                ,
                CoVersion = model.Version

                ,
                CoCategoryId = model.CategoryId

                ,
                CoDescriptionEn = model.DescriptionEn

                ,
                CoDescription = model.Description

                ,
                CoBigImage = model.BigImage

                ,
                CoNormalImage = model.NormalImage

                ,
                CoSmallImage = model.SmallImage

                ,
                CoModelEn = model.ModelEn

                ,
                CoModel = model.Model

                ,
                CoIsRecommend = model.IsRecommend

                ,
                CoSort = model.Sort

                ,
                CoCode = model.Code

                ,
                CoCulture = model.Culture

                ,
                CoPrice = model.Price

                ,
                CoDiscountPrice = model.DiscountPrice

                ,
                CoStockNumber = model.StockNumber

                ,
                CoSoldNumber = model.SoldNumber

                ,
                CoIsClosed = model.IsClosed

                ,
                CoOverview = model.Overview

                ,
                CoOverviewEn = model.OverviewEn

                ,
                CoParameters = model.Parameters

                ,
                CoParametersEn = model.ParametersEn



            };
        }


    }
}



